
var settings = {
    init: function() {
        settings.reconfigureTableView();
        
        this.profile.setup();
    },
    
    profile: {
        
        setup: function() {
            var self = this;
            
            if( $("#profile-form #slug").val() == "" ) {
                $("#profile-form #name").friendurl({id: 'slug'});
            }
            
            $("#profile-form").submit(function(e) {
                e.preventDefault();
                
                self.save();
            });
            
            $("#profile-form :input").change(function() {
                $("#profile-form #submit-btn").removeAttr("disabled");
            });
        },
        
        image_upload_callback:function(p){
             if(p != null){
                var files = p['files'];
                var img_obg = files[0];
                if(typeof img_obg.error == 'undefined'){
                    var img_url = img_obg.url;
                    $("#org_logo img").attr('src', img_url);
                }
            }
        },
        
        save: function() {
            var url = $("#profile-form").attr("action");
            var params = $("#profile-form").serialize();
            
            alertify.log("Saving changes to profile information.");
            
            $.ajax({
                url: url,
                data: params,
                type: 'post',
                dataType: 'json',
                success: function(res) {
                    if( res.status == 'success' ){
                        $("#profile-form #submit-btn").attr("disabled", true);
                        
                        alertify.success("Profile information updated successfully");
                    }
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            });
        }
    },
    
    
    
    reconfigureTableView: function() {
        var self = this;

        $("#table-view tbody tr").click(function() {
            $("#table-view tbody tr").removeClass("selected");

            $(this).addClass("selected");

//            var url = $(this).data('url');
//
//            $.get(url, function(data) {
//                self.showSelectedProfile(data);
//            });
        });

        if ($("#table-view tbody tr.selected").length == 0) {
            $("#table-view tbody tr:first").click();
        }
    },
    showSelectedProfile: function(profile_data) {

        $("#profile-view #editor_view").hide();
        $("#profile-view #profile_view_pane").show().find(".content").html(profile_data);

        this.setProfileViewTitle('');
        this.showEditButtons();

        var iv = new ImagesView();
        iv.quickUploader({
            selector: '#change-img-btn',
            target_view: "#image-box",
            url: $("#change-img-btn").data('url'),
            params: {
                member_id: $("#change-img-btn").data('member-id')
            }
        });
    }
};

$(document).ready(function() {
    settings.init();
});